﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Dropthings.Util;
using OmarALZabir.AspectF;

namespace Dropthings.DAL
{
  public  class BaseQuery
    {
         #region Member fields
        private static string DbConnectionConfigName = "DropthingsConnectionString";

        private Database _GlobalDB;
        private DbConnection _GlobalConnection;

        protected string _QueryStringName = "";
        protected SqlParameter[] _Parameters;
        #endregion

        #region Properties
        private Database GlobalDB
        {
            get { return _GlobalDB; }
            set { _GlobalDB = value; }
        }

        private DbConnection GlobalConnection
        {
            get { return _GlobalConnection; }
            set { _GlobalConnection = value; }
        }

        protected string QueryStringName
        {
            get { return _QueryStringName; }
            set { _QueryStringName = value; }
        }

        protected SqlParameter[] Parameters
        {
            get { return _Parameters; }
            set { _Parameters = value; }
        }
        #endregion

        #region Constructor
        protected BaseQuery()
        {
            this._GlobalDB = null;
            this._GlobalConnection = null;
        }

        protected BaseQuery(Database db, DbConnection conn)
        {
            this.GlobalDB = db;
            this.GlobalConnection = conn;
        }
        #endregion

        #region Static Methods
        public static Database CreateDatabase()
        {
            return DatabaseFactory.CreateDatabase(DbConnectionConfigName);
        }
        #endregion

        #region Public Methods
        public int ExecuteNonQuery()
        {
            try
            {
                Database db = GlobalDB;
                if (db == null)
                {
                    db = DatabaseFactory.CreateDatabase(DbConnectionConfigName);
                }

                DbCommand dbCommand = db.GetSqlStringCommand(QueryStringName);
                if (Parameters != null)
                {
                    dbCommand.Parameters.AddRange(Parameters);
                }
                return db.ExecuteNonQuery(dbCommand);
            }
            catch (Exception e)
            {
                Services.Get<ILogger>().Log("QueryStringName:" + QueryStringName);
                Services.Get<ILogger>().LogException(e);
                return 0;
            }
        }

        public DataSet ExecuteDataset()
        {
            try
            {
                Database db = GlobalDB;
                if (db == null)
                {
                    db = DatabaseFactory.CreateDatabase(DbConnectionConfigName);
                }
                DbCommand dbCommand = db.GetSqlStringCommand(QueryStringName);
                if (Parameters != null)
                {
                    dbCommand.Parameters.AddRange(Parameters);
                }
                return db.ExecuteDataSet(dbCommand);
            }
            catch (Exception e)
            {
                Services.Get<ILogger>().Log("QueryStringName:" + QueryStringName);
                Services.Get<ILogger>().LogException(e);
                return null;
            }
        }

        public object ExecuteScalar()
        {
            try
            {
                Database db = GlobalDB;
                if (db == null)
                {
                    db = DatabaseFactory.CreateDatabase(DbConnectionConfigName);
                }
                DbCommand dbCommand = db.GetSqlStringCommand(QueryStringName);
                if (Parameters != null)
                {
                    dbCommand.Parameters.AddRange(Parameters);
                }
                return db.ExecuteScalar(dbCommand);
            }
            catch (Exception e)
            {
                Services.Get<ILogger>().Log("QueryStringName:" + QueryStringName);
                Services.Get<ILogger>().LogException(e);
                return null;
            }
        }
        #endregion
    }
}
